<?php

namespace App\Http\Controllers\Message;

use App\Http\Requests\MessageNumRequest;
use App\Http\Requests\SwitchReadRequest;
use App\Message;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;


class MessageController extends Controller
{
    //获取用户获得的点赞信息
    public function getLikeInfo(){
         $user_id = auth()->user()->id;
         $status = 0;
         $result = DB::table('messages')
             ->join('posts','messages.post_id','=','posts.id')
             ->select('messages.id','posts.title as articletitle', 'messages.msg as message', 'messages.read as readstatus','messages.created_at as create_time')
             ->where('messages.pull_id','=',$user_id)
            ->where('messages.status','=',$status)
             ->orderBy('messages.created_at','desc')
             ->paginate(11);
        if($result){
            if($result->isEmpty()){
                $data = ['code'=>200,'msg'=>'成功','data'=>"目前没有您的点赞信息"];
                return response()->json($data);
            }else
            {
                $data = ['code'=>200,'msg'=>'成功','data'=>$result];
                return response()->json($data);
            }
        }
        else{
            $data = ['code'=>100,'msg'=>'失败','data'=>'查询失败'];
            return response()->json($data);
        }

    }
    //获取用户获得的积分变化信息
    public function getIntegralInfo(){
        $user_id = auth()->user()->id;
        // dd($user_id);
        $status = 1;
        $result = DB::table('messages')
            ->join('posts','messages.post_id','=','posts.id')
            ->select('messages.id','posts.title as articletitle', 'messages.msg as message', 'messages.read as readstatus','messages.created_at as create_time')
            ->where('messages.pull_id','=',$user_id)
            ->where('messages.status','=',$status)
            ->orderBy('messages.created_at','desc')
            ->paginate(11);
        if($result){
            if($result->isEmpty()){
                $data = ['code'=>200,'msg'=>'成功','data'=>"目前没有您的积分变化信息"];
                return response()->json($data);
            }else
            {
                $data = ['code'=>200,'msg'=>'成功','data'=>$result];
                return response()->json($data);
            }
        }
        else{
            $data = ['code'=>100,'msg'=>'失败','data'=>'查询失败'];
            return response()->json($data);
        }
    }

    //获取用户文章审核信息
    public function getAuditInfo(){
        $user_id = auth()->user()->id;
        $status = 2;
        $result = DB::table('messages')
            ->join('posts','messages.post_id','=','posts.id')
            ->select('messages.id','messages.post_id',
                'posts.title as articletitle', 'messages.msg as message',
                'messages.read as readstatus','messages.feedback',
                'messages.created_at as create_time')
            ->where('messages.pull_id','=',$user_id)
            ->where('messages.status','=',$status)
            ->orderBy('messages.created_at','desc')
            ->paginate(11);
        if($result){
            if($result->isEmpty()){
                $data = ['code'=>200,'msg'=>'成功','data'=>"目前没有您的文章审核信息"];
                return response()->json($data);
            }else
            {
                $data = ['code'=>200,'msg'=>'成功','data'=>$result];
                return response()->json($data);
            }
        }
        else{
            $data = ['code'=>100,'msg'=>'失败','data'=>'查询失败'];
            return response()->json($data);
        }

    }

    //反馈信息获取
    public function getFeedbcakInfo(){
        $user_id = auth()->user()->id;
        $status = 3;
        $result = DB::table('messages')
            ->join('posts','messages.post_id','=','posts.id')
            ->join('users','messages.push_id','=','users.id')
            ->select('messages.id','messages.post_id',
                'posts.title as articletitle', 'messages.msg as message',
                'messages.read as readstatus','messages.feedback',
                'users.user_name as pushname','messages.created_at as create_time')
            ->where('messages.pull_id','=',$user_id)
            ->where('messages.status','=',$status)
            ->orderBy('messages.created_at','desc')
            ->paginate(11);
        if($result){
            if($result->isEmpty()){
                $data = ['code'=>200,'msg'=>'成功','data'=>"目前没有您的反馈信息"];
                return response()->json($data);
            }else
            {
                $data = ['code'=>200,'msg'=>'成功','data'=>$result];
                return response()->json($data);
            }
        }
        else{
            $data = ['code'=>100,'msg'=>'失败','data'=>'查询失败'];
            return response()->json($data);
        }

    }

    //全部信息统计
    public function messageAllNum(MessageNumRequest $request){
        $user_id = auth()->user()->id;
        $status = $request->status;
        $result = DB::table('messages')
            ->where('pull_id','=',$user_id)
            ->where('status','=',$status)
            ->get()->count();
        if($result == 0){
            $data = ['code'=>200,'msg'=>'成功','data'=>0];
            return response()->json($data);
        };
        if($result!=0){

                $data = ['code'=>200,'msg'=>'成功','data'=>$result];
                return response()->json($data);
        }
        else{
            $data = ['code'=>100,'msg'=>'失败','data'=>'查询失败'];
            return response()->json($data);
        }

    }

    //未读信息统计
    public function messageUnreadNum(MessageNumRequest $request){
        $user_id = auth()->user()->id;
        $status = $request->status;
        $result = DB::table('messages')
            ->where('pull_id','=',$user_id)
            ->where('status','=',$status)
            ->where('read','=',0)
            ->get()->count();
        if($result == 0){
            $data = ['code'=>200,'msg'=>'成功','data'=>0];
            return response()->json($data);
        };
        if($result!=0){

            $data = ['code'=>200,'msg'=>'成功','data'=>$result];
            return response()->json($data);
        }
        else{
            $data = ['code'=>100,'msg'=>'失败','data'=>'查询失败'];
            return response()->json($data);
        }
    }

    //获取点赞信息未读
    public function getLikeNoread(){
        $user_id = auth()->user()->id;
        $status = 0;
        $result = DB::table('messages')
            ->join('posts','messages.post_id','=','posts.id')
            ->select('messages.id','posts.title as articletitle',
                'messages.msg as message',
                'messages.read as readstatus','messages.created_at as create_time')
            ->where('messages.pull_id','=',$user_id)
            ->where('messages.status','=',$status)
            ->where('messages.read','=',0)
            ->orderBy('messages.created_at','desc')
            ->paginate(11);
        if($result){
            if($result->isEmpty()){
                $data = ['code'=>200,'msg'=>'成功','data'=>"目前没有未读信息"];
                return response()->json($data);
            }else
            {
                $data = ['code'=>200,'msg'=>'成功','data'=>$result];
                return response()->json($data);
            }
        }
        else{
            $data = ['code'=>100,'msg'=>'失败','data'=>'查询失败'];
            return response()->json($data);
        }

    }

    //获取用户积分变动未读
    public function getIntegralNoread(){
        $user_id = auth()->user()->id;
        $status = 1;
        $result = DB::table('messages')
            ->join('posts','messages.post_id','=','posts.id')
            ->select('messages.id','posts.title as articletitle',
                'messages.msg as message',
                'messages.read as readstatus','messages.created_at as create_time')
            ->where('messages.pull_id','=',$user_id)
            ->where('messages.status','=',$status)
            ->where('messages.read',0)
            ->orderBy('messages.created_at','desc')
            ->paginate(11);
        if($result){
            if($result->isEmpty()){
                $data = ['code'=>200,'msg'=>'成功','data'=>"目前没有未读信息"];
                return response()->json($data);
            }else
            {
                $data = ['code'=>200,'msg'=>'成功','data'=>$result];
                return response()->json($data);
            }
        }
        else{
            $data = ['code'=>100,'msg'=>'失败','data'=>'查询失败'];
            return response()->json($data);
        }
    }

  //获取用户文章审核未读
    public function getAuditNoread(){
        $user_id = auth()->user()->id;
        $status = 2;
        $result = DB::table('messages')
            ->join('posts','messages.post_id','=','posts.id')
            ->select('messages.id','messages.post_id',
                'posts.title as articletitle', 'messages.msg as message',
                'messages.read as readstatus','messages.feedback',
                'messages.created_at as create_time')
            ->where('messages.pull_id','=',$user_id)
            ->where('messages.status','=',$status)
            ->where('messages.read',0)
            ->orderBy('messages.created_at','desc')
            ->paginate(11);
        if($result){
            if($result->isEmpty()){
                $data = ['code'=>200,'msg'=>'成功','data'=>"目前没有未读信息"];
                return response()->json($data);
            }else
            {
                $data = ['code'=>200,'msg'=>'成功','data'=>$result];
                return response()->json($data);
            }
        }
        else{
            $data = ['code'=>100,'msg'=>'失败','data'=>'查询失败'];
            return response()->json($data);
        }

    }

    //反馈信息未读
    public function getFeedbcakNoread(){
        $user_id = auth()->user()->id;
        $status = 3;
        $result = DB::table('messages')
            ->join('posts','messages.post_id','=','posts.id')
            ->join('users','messages.push_id','=','users.id')
            ->select('messages.id','messages.post_id',
                'posts.title as articletitle', 'messages.msg as message',
                'messages.read as readstatus','messages.feedback',
                'users.user_name as pushname','messages.created_at as create_time')
            ->where('messages.pull_id','=',$user_id)
            ->where('messages.status','=',$status)
            ->where('messages.read',0)
            ->orderBy('messages.created_at','desc')
            ->paginate(11);
        if($result){
            if($result->isEmpty()){
                $data = ['code'=>200,'msg'=>'成功','data'=>"目前没有未读信息"];
                return response()->json($data);
            }else
            {
                $data = ['code'=>200,'msg'=>'成功','data'=>$result];
                return response()->json($data);
            }
        }
        else{
            $data = ['code'=>100,'msg'=>'失败','data'=>'查询失败'];
            return response()->json($data);
        }

    }

    //切换已读信息
    public function switchRead(SwitchReadRequest $request){
        $id = $request->id;
        $stat = Message::find($id);
        if($stat){
            if($stat->read == 0){
                $stat->read = 1;
                $result = $stat->save();
                if($result){
                    $data = ['code'=>200,'msg'=>'成功','data'=>"已读信息"];
                    return response()->json($data);
                }
                else{
                    $data = ['code'=>100,'msg'=>'失败','data'=>'修改失败'];
                    return response()->json($data);
                }
            }
            else{
                $data = ['code'=>100,'msg'=>'失败','data'=>'状态错误'];
                return response()->json($data);
            }
        }
        else{
            $data = ['code'=>100,'msg'=>'失败','data'=>'查询文章失败'];
            return response()->json($data);
        }
    }

}
